import java.util.*;

public class Main {

    public static String solution(String s, int k) {
        // write code here
        if (k <= 0) {
            return s;
        }

        StringBuilder res = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c == 'a') {
                res.append("bc");
            }
            if (c == 'b') {
                res.append("ca");
            }
            if (c == 'c') {
                res.append("ab");
            }
        }
        k -= 1;

        return solution(res.toString(), k);
    }

    public static String solution1(String s, int k) {
        if (k <= 0) {
            return s;
        }

        // 使用HashMap存储映射规则
        Map<Character, String> mapping = new HashMap<>();
        mapping.put('a', "bc");
        mapping.put('b', "ca");
        mapping.put('c', "ab");

        // 使用StringBuilder收集字符提高效率
        StringBuilder res = new StringBuilder();
        for (char c : s.toCharArray()) {
            res.append(mapping.get(c));
        }

        // 递归调用
        return solution1(res.toString(), k - 1);
    }

    public static void main(String[] args) {
        System.out.println(solution("abc", 2).equals("caababbcbcca"));
        System.out.println(solution("abca", 3).equals("abbcbccabccacaabcaababbcabbcbcca"));
        System.out.println(solution("cba", 1).equals("abcabc"));

        System.out.println(solution1("abc", 2).equals("caababbcbcca"));
        System.out.println(solution1("abca", 3).equals("abbcbccabccacaabcaababbcabbcbcca"));
        System.out.println(solution1("cba", 1).equals("abcabc"));
    }
}